using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.DB;
using WP.Modules;
using WP.Commons;

namespace WP.MF.Form
{
    public partial class frmMMessageDictMtn : BaseMtn
    {
        private DBAccess dbAccess = null;
        private bool mblnListEndFill = false;
        public frmMMessageDictMtn()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            dbAccess = new DBAccess(false, false);
            this.MinimumSize = this.Size;
        }

        private void frmMMessageDictMtn_Load(object sender, EventArgs e)
        {
            ShowLanguageMenu();
            FillList();
        }
        private void frmMMessageDictMtn_Resize(object sender, EventArgs e)
        {
            dgList.Focus();
        }
        private void frmMMessageDictMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            dbAccess.CloseCon();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }
        private void dgList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string strValue = string.Empty;
            if (dgList.Rows.Count > 0 && mblnListEndFill)
            {
                strValue = dgList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                if (strValue.Length > 256)
                {
                    dgList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = strValue.Substring(0, 256);
                }
                UpdateChange(e.RowIndex, e.ColumnIndex);
            }
        }

        public void ShowForm()
        {
            this.MdiParent = SysVar.MDIParentForm;
            this.Show();
        }
        private void FillList()
        {
            string strSql = string.Empty;
            strSql = "select * from mLanguage where Language<>'English' order by LanguageSeqNo";
            DataTable DtLanguage = dbAccess.GetDataTable(strSql);
            strSql = "select recid,rtrim(ltrim(Descript)) as Descript,";
            foreach (DataRow DrLanguage in DtLanguage.Rows)
            {
                strSql += "rtrim(ltrim(Language" + DrLanguage["LanguageSeqNo"].ToString() + "Descript)) as " + DrLanguage["Language"].ToString().Trim() + ",";
            }
            strSql = strSql.Substring(0, strSql.Length - 1);
            strSql += " from mInfoDict";
            mblnListEndFill = false;
            DataTable DtLanguageDict = dbAccess.GetDataTable(strSql);
            dgList.DataSource = DtLanguageDict;
            AdjustGrid();
            mblnListEndFill = true;
        }
        private void AdjustGrid()
        {
            this.dgList.RowHeadersVisible = false;
            this.dgList.ColumnHeadersHeight = 22;
            this.dgList.Columns["RecID"].Visible = false;
            this.dgList.Columns["RecID"].Width = 0;
            this.dgList.Columns["RecID"].Frozen = true;
            this.dgList.Columns["Descript"].ReadOnly = true;
            this.dgList.Columns["Descript"].Frozen = true;
            this.dgList.Columns["Descript"].DefaultCellStyle.BackColor = System.Drawing.SystemColors.Control;
            int intWidth = 0;
            for (int i = 1; i <= dgList.Columns.Count - 1; i++)
            {
                dgList.Columns[i].Width = 300;
                intWidth += dgList.Columns[i].Width;
            }
            for (int i = 0; i <= dgList.Rows.Count - 1; i++)
            {
                dgList.Rows[i].Height = 18;
            }
            if (intWidth < this.Width)
            {
                int intLastColumnWidth = 0;
                for (int i = 1; i < dgList.Columns.Count - 1; i++)
                {
                    intLastColumnWidth += dgList.Columns[i].Width;
                }
                dgList.Columns[dgList.Columns.Count - 1].Width = this.Width - intLastColumnWidth - 35;
            }
        }
        private bool UpdateChange(int irowNo, int iColNo)
        {
            string strSql = string.Empty;
            int intText = 0;
            strSql = "select language,languageSeqNo from mLanguage where Language<>'English' order by LanguageSeqNo";
            DataTable DtLanguage = dbAccess.GetDataTable(strSql);
            foreach (DataRow DrLanguage in DtLanguage.Rows)
            { 
                 if(DrLanguage["language"].ToString().Trim()==dgList.Columns[iColNo].HeaderText)
                 {
                     intText=CvtFunc.ToInt(DrLanguage["languageSeqNo"]);
                 }
            }
            strSql = "update mInfoDict set Language" + intText.ToString() + "Descript='" + dgList.Rows[irowNo].Cells[iColNo].Value.ToString() + "' where RecID=" + dgList.Rows[irowNo].Cells[0].Value.ToString();
            dbAccess.ExecuteSql(strSql);
            return true;
        }
        private void LanguageMenuItem_Click(object sender, EventArgs e)
        {
            btnLanguage.ShowDropDown();
            ToolStripMenuItem ToolMenu = (ToolStripMenuItem)sender;
            ToolMenu.Checked = (ToolMenu.Checked ? false : true);
            dgList.Columns[ToolMenu.Text.Trim()].Visible = ToolMenu.Checked;
        }
        private void ShowLanguageMenu()
        {
            DataView dv = dbAccess.GetDataTable("select * from mLanguage").DefaultView;
            dv.RowFilter = "LanguageSeqNo=2";
            if (dv.Count != 0)
            {
                language2ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language2ToolStripMenuItem.Checked = true;
                language2ToolStripMenuItem.Visible = true;
            }
            else
            {
                language2ToolStripMenuItem.Checked = false;
                language2ToolStripMenuItem.Visible = false;
            }
            dv.RowFilter = "LanguageSeqNo=3";
            if (dv.Count != 0)
            {
                language3ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language3ToolStripMenuItem.Checked = true;
                language3ToolStripMenuItem.Visible = true;
            }
            else
            {
                language3ToolStripMenuItem.Visible = false;
                language3ToolStripMenuItem.Checked = false;
            }
            dv.RowFilter = "LanguageSeqNo=4";
            if (dv.Count != 0)
            {
                language4ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language4ToolStripMenuItem.Checked = true;
                language4ToolStripMenuItem.Visible = true;
            }
            else
            {
                language4ToolStripMenuItem.Checked = false;
                language4ToolStripMenuItem.Visible = false;
            }
            dv.RowFilter = "LanguageSeqNo=5";
            if (dv.Count != 0)
            {
                language5ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language5ToolStripMenuItem.Checked = true;
                language5ToolStripMenuItem.Visible = true;
            }
            else
            {
                language5ToolStripMenuItem.Checked = false;
                language5ToolStripMenuItem.Visible = false;
            }
            dv.RowFilter = "LanguageSeqNo=6";
            if (dv.Count != 0)
            {
                language6ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language6ToolStripMenuItem.Checked = true;
                language6ToolStripMenuItem.Visible = true;
            }
            else
            {
                language6ToolStripMenuItem.Checked = false;
                language6ToolStripMenuItem.Visible = false;
            }
        }

    }
}